import Mock from 'mockjs'

var data=Mock.mock(
    {
        "list|10":[{
            id:"@increment()",
            dt:"@date()",
            name:"@cname()",
            addrres:"@city()"    
        }]
    })
//console.info(data);

var {list}=data;

function getUrl(url,name){
    var index=url.indexOf('?');
    var zhi=url.substr(index+1);
    //currentPage=1&pageSize=1
    var zhis= zhi.split('&');
    //[currentPage=1, pageSize=1]
    for (let index = 0; index < zhis.length; index++) {
        const element = zhis[index];
        //currentPage=1
       var zhi2= element.split('=');
       //[currentPage,1]
       if(zhi2[0]==name){
         return  zhi2[1];
       }
    }
}

//定义get拦截方法
Mock.mock(/\/api\/get\/users/,'get',(options)=>{
    //url:api/get/users?currentPage=1&pageSize=1
    var currentPage=getUrl(options.url,"currentPage");
    var pageSize=getUrl(options.url,"pageSize");
    //1 1
    var dt=list.slice(((currentPage-1)*pageSize),currentPage*pageSize) //分页公式:包前不包后的下标
    //分页公式
    //1页 10条  0-10   
    //2页 10条  10-20
    //3页 10条  20-30
    //第1个数: (当前页-1)*每页显示数
    //第2个数：当前页*每页显示数
    return{
        "data": dt,
        "total":list.length,
        "meta": {
            "msg": "获取用户数据成功",
            "status": 200
        }
    }
})